package br.ucb.sigplan.action.impl;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import br.ucb.sigplan.action.Action;
import br.ucb.sigplan.action.impl.service.CadastraConteudosTurmaService;
import br.ucb.sigplan.beans.Conteudo;
import br.ucb.sigplan.beans.Turma;
import br.ucb.sigplan.crud.ConteudoTurmaCrud;
import br.ucb.sigplan.util.ConteudoUtil;

public class CadastraConteudoTurmaAction implements Action {

	@SuppressWarnings("unchecked")
	@Override
	public String execute(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		HttpSession session = req.getSession();
		List<Conteudo> conteudoSelecionados =  (List<Conteudo>) session.getAttribute("conteudosTurma");
		if (conteudoSelecionados == null || conteudoSelecionados.isEmpty()) {
			req.setAttribute("msg", "erro.selecaoConteudos");
			return "/jsp/professor/conteudo/profCadastraConteudoTurma.jsp";
		}
		ConteudoUtil.removeConteudosCadastrados(conteudoSelecionados, ConteudoTurmaCrud.findConteudoByTurma((Integer) req.getSession().getAttribute("turma")));
		CadastraConteudosTurmaService.insertAll(conteudoSelecionados, new Turma((Integer) session.getAttribute("turma")));
		session.removeAttribute("conteudos");
		return "/jsp/professor/plano/profCadastraPlano.jsp";
	}
}
